using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace Weathertastic
{ 
    class Locations : ArrayList
    {
        public Locations()
        {
            try
            {
                XmlDocument xFile = new XmlDocument();
                xFile.Load(Path.Combine(Application.StartupPath, "settings.xml"));
                XmlNodeList NodesLocation = xFile.SelectNodes("//location");
                foreach (XmlNode Node in NodesLocation)
                {
                    string _station = Node.Attributes["station"].Value;
                    string _name = Node.Attributes["name"].Value;
                    bool _default = Convert.ToBoolean(Node.Attributes["default"].Value);
                    Add(new Location(_station, _name, _default));
                }

                NodesLocation = null;
                xFile = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public override void Remove(object obj)
        {
            string sStation = (string)obj;
            try
            {
                XmlDocument xFile = new XmlDocument();
                xFile.Load(Path.Combine(Application.StartupPath, "settings.xml"));
                XmlNode NodeRoot = xFile.SelectSingleNode("//locations");
                XmlNode NodeToFind = xFile.SelectSingleNode("//location[@station=\"" + sStation + "\"]");
                NodeRoot.RemoveChild(NodeToFind);

                xFile.Save(Path.Combine(Application.StartupPath, "settings.xml"));
                NodeToFind = null;
                xFile = null;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public void SetDefault(string sStation)
        {
            try
            {
                XmlDocument xFile = new XmlDocument();
                xFile.Load(Path.Combine(Application.StartupPath, "settings.xml"));

                XmlNodeList Nodes = xFile.SelectNodes("//location[@default=\"True\"]");
                if (Nodes.Count > 0)
                {
                    foreach (XmlNode Node in Nodes)
                    {
                        Node.Attributes["default"].Value = false.ToString();
                    }
                }

                XmlNode NodeToFind = xFile.SelectSingleNode("//location[@station=\"" + sStation + "\"]");
                if (NodeToFind != null)
                {
                    XmlAttribute AttributeDefault = xFile.CreateAttribute("default");
                    AttributeDefault.Value = true.ToString();
                    NodeToFind.Attributes.Append(AttributeDefault);
                }

                xFile.Save(Path.Combine(Application.StartupPath, "settings.xml"));
                Nodes = null;
                xFile = null;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

    class Location
    {
        public string Station = string.Empty;
        public string Name = string.Empty;
        public bool Default = false;

        public Location(string StationIn, string NameIn, bool DefaultIn)
        {
            this.Station = StationIn;
            this.Name = NameIn;
            this.Default = DefaultIn;
        }
    }
}
